package com.ruoyi.reservationserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.reservationserver.dao.StadiumDao;
import com.ruoyi.system.mapper.StadiumStatisticMapper;
import com.ruoyi.system.domain.Stadium;
import com.ruoyi.system.domain.StadiumStatistic;
import com.ruoyi.reservationserver.service.StadiumService;
import com.ruoyi.reservationserver.service.StadiumStatisticService;
import com.ruoyi.reservationserver.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 球馆表(Stadium)表控制层
 *
 * @author makejava
 * @since 2023-07-04 16:21:49
 */
//@RestController
//@RequestMapping("stadium")
public class StadiumController {
    /**
     * 服务对象
     */
    @Resource
    private StadiumService stadiumService;

    @Resource
    private StadiumDao stadiumDao;

    @Resource
    private StadiumStatisticService stadiumStatisticService;

    @Resource
    private StadiumStatisticMapper stadiumStatisticMapper;

    /**
     * 获取分页球馆信息
     * @param page
     * @param count
     * @return
     */
    @GetMapping
    public Result index(@RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "count", defaultValue = "10") int count) {
        PageInfo<Stadium> pageInfo = stadiumService.selectStadiumWithPage(page, count);
        return Result.success(pageInfo);
    }

    /**
     * 通过球馆名称查询球馆信息
     * @param stadiumName
     * @return
     */
    @GetMapping("selectStadiumListByStadiumName")
    public Result selectStadiumListByStadiumName(@RequestParam String stadiumName){
        List<Stadium> stadiumList;
        if (stadiumName==null){
            return Result.error("不能输入空信息");
        }
        stadiumList = stadiumDao.selectStadiumListByCourtName(stadiumName);
        return Result.success(stadiumList);
    }

    /**
     * 新增数据
     *
     * @param stadium 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(@RequestBody Stadium stadium) {
        System.out.println("球馆："+stadium);
        this.stadiumService.insert(stadium);

        QueryWrapper<StadiumStatistic> wrapper = new QueryWrapper<>();
        wrapper.eq("stadium_statistic_id",1);
        StadiumStatistic stadiumStatistic = stadiumStatisticService.getOne(wrapper);
        stadiumStatistic.setTotalStadiumNum(stadiumService.count());
        stadiumStatisticService.update(stadiumStatistic);
        return Result.success();
    }

    /**
     * 编辑数据
     *
     * @param stadium 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Stadium stadium) {
        try {
            System.out.println(stadium);
            if (stadium==null){
                return Result.error("数据不能为空");
            }
            System.out.println(stadium.getStadiumId());
            if(stadium.getStadiumId()==null){
                return Result.error("id不能为空");
            }
            this.stadiumService.update(stadium);
            return Result.success();

        }catch (Exception e){
            return Result.error("更新失败");
        }

    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/del")
    public Result deleteById(@RequestParam("stadiumId") Integer id) {
        StadiumStatistic stadiumStatistic;
        try {
            if(this.stadiumService.deleteById(id)){
                QueryWrapper<StadiumStatistic> wrapper = new QueryWrapper<>();
                wrapper.eq("stadium_statistic_id",1);
                stadiumStatistic = stadiumStatisticService.getOne(wrapper);
                stadiumStatistic.setTotalStadiumNum(stadiumService.count());
                stadiumStatisticService.update(stadiumStatistic);
                return Result.success();
            }else {
                return Result.error("id不存在");
            }


        }catch (Exception e) {
//            System.out.println(e);
            return Result.error("删除失败");
        }

    }

}

